#include "qwincoordinatechange.h"
#include "ui_qwincoordinatechange.h"

#include <QRegularExpressionValidator>


QWinCoordinateChange::QWinCoordinateChange(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QWinCoordinateChange)
{
    ui->setupUi(this);

    //坐标系设置
    Ellipsoid ell_bj54(6378245.0, 1/298.3);
    Ellipsoid ell_cscg2000(6378137.0, 1/298.257222101);
    prj_bj54.SetDatum(ell_bj54);
    prj_bj54.SetCentralMeridian(DMSToRadian(123));
    prj_bj54.SetFalseEasting(500000);
    prj_bj54.SetFalseNorthing(0);
    prj_cscg2000.SetDatum(ell_cscg2000);
    prj_cscg2000.SetCentralMeridian(DMSToRadian(123));
    prj_cscg2000.SetFalseEasting(500000);
    prj_cscg2000.SetFalseNorthing(0);

    // LineEdit控件限制输入
    ui->edtZone->setValidator(new QRegularExpressionValidator(QRegularExpression("[0-9]{0,64}")));
    ui->edtFalseEasting->setValidator(new QRegularExpressionValidator(QRegularExpression("[0-9-]{0,64}")));
    ui->edtFalseNorthing->setValidator(new QRegularExpressionValidator(QRegularExpression("[0-9-]{0,64}")));
    ui->edt_bj54_x->setValidator(new QRegularExpressionValidator(QRegularExpression("[-.0-9]{0,64}")));
    ui->edt_bj54_y->setValidator(new QRegularExpressionValidator(QRegularExpression("[-.0-9]{0,64}")));
    ui->edt_cgcs2000_x->setValidator(new QRegularExpressionValidator(QRegularExpression("[-.0-9]{0,64}")));
    ui->edt_cgcs2000_y->setValidator(new QRegularExpressionValidator(QRegularExpression("[-.0-9]{0,64}")));




    //井位p1 在北京54坐标系下的 高斯坐标、大地坐标、地心坐标
    CartesianCoordinate bj54_p1_gs(5056566.0997,594708.0857,0);
    GeodeticCoordinate bj54_p1_LB=CoordinateTransformation::InverseGauss(prj_bj54,bj54_p1_gs);
    GeocentricCoordinate bj54_p1_central = CoordinateTransformation::GeodeticToGeocentric(prj_bj54,bj54_p1_LB);
    //井位p2 在北京54坐标系下的 高斯坐标、大地坐标、地心坐标
    CartesianCoordinate bj54_p2_gs(5055453.1126,594553.0853,0);
    GeodeticCoordinate bj54_p2_LB=CoordinateTransformation::InverseGauss(prj_bj54,bj54_p2_gs);
    GeocentricCoordinate bj54_p2_central = CoordinateTransformation::GeodeticToGeocentric(prj_bj54,bj54_p2_LB);
    //井位p3 在北京54坐标系下的 高斯坐标、大地坐标、地心坐标
    CartesianCoordinate bj54_p3_gs(5056263.5503,594717.5884,0);
    GeodeticCoordinate bj54_p3_LB=CoordinateTransformation::InverseGauss(prj_bj54,bj54_p3_gs);
    GeocentricCoordinate bj54_p3_central = CoordinateTransformation::GeodeticToGeocentric(prj_bj54,bj54_p3_LB);

    QVector<BasePoint> inputVector;
    inputVector.push_back(bj54_p1_central);
    inputVector.push_back(bj54_p2_central);
    inputVector.push_back(bj54_p3_central);

    //井位p1 在国家2000坐标系下的 高斯坐标、大地坐标、地心坐标
    CartesianCoordinate cgcs2000_p1_gs(5056525,594776,0);
    GeodeticCoordinate cgcs2000_p1_LB=CoordinateTransformation::InverseGauss(prj_cscg2000,cgcs2000_p1_gs);
    GeocentricCoordinate cgcs2000_p1_central = CoordinateTransformation::GeodeticToGeocentric(prj_cscg2000,cgcs2000_p1_LB);
    //井位p2 在国家2000坐标系下的 高斯坐标、大地坐标、地心坐标
    CartesianCoordinate cgcs2000_p2_gs(5055412,594621,0);
    GeodeticCoordinate cgcs2000_p2_LB=CoordinateTransformation::InverseGauss(prj_cscg2000,cgcs2000_p2_gs);
    GeocentricCoordinate cgcs2000_p2_central = CoordinateTransformation::GeodeticToGeocentric(prj_cscg2000,cgcs2000_p2_LB);
    //井位p3 在国家2000坐标系下的 高斯坐标、大地坐标、地心坐标
    CartesianCoordinate cgcs2000_p3_gs(5056222.446956,594785.50389,0);
    GeodeticCoordinate cgcs2000_p3_LB=CoordinateTransformation::InverseGauss(prj_cscg2000,cgcs2000_p3_gs);
    GeocentricCoordinate cgcs2000_p3_central = CoordinateTransformation::GeodeticToGeocentric(prj_cscg2000,cgcs2000_p3_LB);

    QVector<BasePoint> outputVector;
    outputVector.push_back(cgcs2000_p1_central);
    outputVector.push_back(cgcs2000_p2_central);
    outputVector.push_back(cgcs2000_p3_central);

    //计算七参数
    sp.Calculate(inputVector.data(),outputVector.data(),inputVector.size());
//    qDebug()<<"DX:"<<QString::number(sp.DX,14,14)<<",DY:"<<QString::number(sp.DY,14,14)<<",DZ:"<<QString::number(sp.DZ,14,14)
//           <<",RX:"<<QString::number(sp.RX,14,14)<<",RY:"<<QString::number(sp.RY,14,14)<<",RZ:"<<QString::number(sp.RZ,14,14)
//          <<",PPM:"<<QString::number(sp.K,14,14);


    //测试七参数
    prj_bj54.SetCentralMeridian(DMSToRadian(123));
    prj_cscg2000.SetCentralMeridian(DMSToRadian(123));
    //井位p1 在北京54坐标系下的 高斯坐标、大地坐标、地心坐标
    CartesianCoordinate bj54_p4_gs(5056383.5509,594492.5915,0);
    qDebug()<<"bj54_p4_gs:"<<QString::number(bj54_p4_gs.x,14,9)<<","<<QString::number(bj54_p4_gs.y,14,9);
    GeodeticCoordinate bj54_p4_LB=CoordinateTransformation::InverseGauss(prj_bj54,bj54_p4_gs);
    qDebug()<<"bj54_p4_LB:"<<QString::number(RadianToDMS(bj54_p4_LB.x),14,9)<<","<<QString::number(RadianToDMS(bj54_p4_LB.y),14,9)<<","<<QString::number(bj54_p4_LB.z,14,9);
    GeocentricCoordinate bj54_p4_central = CoordinateTransformation::GeodeticToGeocentric(prj_bj54,bj54_p4_LB);
    qDebug()<<"bj54_p4_central:"<<QString::number(bj54_p4_central.x,14,9)<<","<<QString::number(bj54_p4_central.y,14,9)<<","<<QString::number(bj54_p4_central.z,14,9);

    BasePoint cgcs2000_p4_central = sp.Transform(bj54_p4_central);
    BasePoint cgcs2000_p4_LB = CoordinateTransformation::GeocentricToGeodetic(prj_cscg2000,cgcs2000_p4_central);
    qDebug()<<"cgcs2000_p4_LB:"<<QString::number(RadianToDMS(cgcs2000_p4_LB.x),14,9)<<","<<QString::number(RadianToDMS(cgcs2000_p4_LB.y),14,9)<<","<<QString::number(cgcs2000_p4_LB.z,14,9);
    BasePoint cgcs2000_p4_gs=CoordinateTransformation::PositiveGauss(prj_cscg2000,cgcs2000_p4_LB);
    qDebug()<<"cgcs2000_p4_gs:"<<QString::number(cgcs2000_p4_gs.x,14,9)<<","<<QString::number(cgcs2000_p4_gs.y,14,9);

}

QWinCoordinateChange::~QWinCoordinateChange()
{
    delete ui;
}

void QWinCoordinateChange::on_btn_change_clicked()
{
    //带号转中央子午线
    int zone = ui->edtZone->text().toUInt();
    int centralmeridian = zone*6-3;
    qDebug()<<"中央子午线:"<<centralmeridian;
    //设置坐标系
    prj_bj54.SetCentralMeridian(DMSToRadian(centralmeridian));
    prj_bj54.SetFalseEasting(ui->edtFalseEasting->text().toInt());
    prj_bj54.SetFalseNorthing(ui->edtFalseNorthing->text().toInt());
    prj_cscg2000.SetCentralMeridian(DMSToRadian(centralmeridian));
    prj_cscg2000.SetFalseEasting(ui->edtFalseEasting->text().toInt());
    prj_cscg2000.SetFalseNorthing(ui->edtFalseNorthing->text().toInt());

    //坐标转换
    double bj54_x = ui->edt_bj54_x->text().toDouble();
    double bj54_y = ui->edt_bj54_y->text().toDouble();
    CartesianCoordinate bj54_p4_gs(bj54_x,bj54_y,0);//高斯坐标
    GeodeticCoordinate bj54_p4_LB=CoordinateTransformation::InverseGauss(prj_bj54,bj54_p4_gs);//经纬度
    GeocentricCoordinate bj54_p4_central = CoordinateTransformation::GeodeticToGeocentric(prj_bj54,bj54_p4_LB);//地心坐标
    BasePoint cgcs2000_p4_central = sp.Transform(bj54_p4_central);
    BasePoint cgcs2000_p4_LB = CoordinateTransformation::GeocentricToGeodetic(prj_cscg2000,cgcs2000_p4_central);
    BasePoint cgcs2000_p4_gs=CoordinateTransformation::PositiveGauss(prj_cscg2000,cgcs2000_p4_LB);

    ui->edt_cgcs2000_x->setText(QString::number(cgcs2000_p4_gs.x,14,6));
    ui->edt_cgcs2000_y->setText(QString::number(cgcs2000_p4_gs.y,14,6));
}

